﻿Imports System.ComponentModel
Imports System.Collections.ObjectModel
Imports FuncClasses
Imports System.Threading.Tasks
Imports System.Windows.Threading

Public Class DeliveryOrdersViewModel
    Implements INotifyPropertyChanged

    Private _customer As Customer
    Private _orders As ObservableCollection(Of OrderPreviewModel)
    Private _dispatcher As Dispatcher
    Private _loader As IDatabase
    Private _user As User

    Public Sub New(_customer As Customer, dispatcher As Dispatcher, loader As IDatabase, user As User)
        _dispatcher = dispatcher
        _loader = loader
        Customer = _customer
        _user = user
    End Sub

    Public Property DeliveryOrders As ObservableCollection(Of OrderPreviewModel)
        Get
            Return _orders
        End Get
        Set(value As ObservableCollection(Of OrderPreviewModel))
            _orders = value
            NotifyPropertyChanged("DeliveryOrders")
        End Set
    End Property

    Public Property Customer As Customer
        Get
            Return _customer
        End Get
        Set(value As Customer)
            If value IsNot Nothing Then
                If value.Equals(Customer) = False Then
                    _customer = value
                    NotifyPropertyChanged("Customer")
                    Refresh()
                End If
            End If
        End Set
    End Property

    Public Sub Refresh()
        If Customer IsNot Nothing Then
            _orders = New ObservableCollection(Of OrderPreviewModel)
            For Each o As OrderPreviewModel In _loader.GetOrdersByCustomer(Customer.CustomerID, _user)
                If o.OrderTyp = Dokumentart.Lieferschein Then
                    _orders.Add(o)
                End If
            Next
        End If
    End Sub

    Protected Sub NotifyPropertyChanged(info As [String])
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
